package net.easyjava.tools.date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTools {

    private static Logger logger = LoggerFactory.getLogger(DateTools.class);

    @Deprecated
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT = "yyyyMMddHHmmss";
    public static final String DATE_MIN_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String DATE_TIME_FORMAT2 = "MM/dd/yyyy HH:mm";
    public static final String DATE_FILE_PATH = "yyyy/MM/dd/";

    private DateTools() {
    }

    private static final String[] FORMATS = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm",
            "yyyy-MM-dd HH:mm:ss", "HH:mm", "HH:mm:ss", "yyyy-MM","yyyyMMdd"};

    public static Date convert(String str) {
        if (str != null && str.length() > 0) {
            if (str.length() > 10 && str.charAt(10) == 'T')
                str = str.replace('T', ' '); // ȥ��json-lib�ӵ�T��ĸ
            for (String format : FORMATS) {
                if (str.length() == format.length()) {
                    try {
                        if (logger.isDebugEnabled())
                            logger.debug("convert " + str + " to date format "
                                    + format);

                        Date date = new SimpleDateFormat(format).parse(str);

                        if (logger.isDebugEnabled())
                            logger.debug("****" + date + "****");

                        return date;
                    } catch (ParseException e) {
                        logger.warn(e.getMessage());
                    }
                }
            }
        }
        return null;
    }

    public static Date convert(String str, String format) {
        if (!StringUtils.isEmpty(str)) {
            try {
                Date date = new SimpleDateFormat(format).parse(str);
                return date;
            } catch (ParseException e) {
                logger.warn(e.getMessage());
            }
        }
        return null;
    }

    public static String convert(Date date) {
        return convert(date, DATE_TIME_FORMAT);
    }

    public static String convert(Date date, String dateFormat) {
        if (date == null)
            return null;

        if (null == dateFormat)
            dateFormat = DATE_TIME_FORMAT;

        return new SimpleDateFormat(dateFormat).format(date);
    }

    public static String formate(Object date) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(DATE_TIME_FORMAT).format(date);
    }

    public static Date getYesterdayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getYesterdayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Long formatStringToTimestamp(String time,String fotmat){
        SimpleDateFormat sdf  = new SimpleDateFormat(fotmat);
        try {
            Date date = sdf.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            logger.error(e.getMessage(),e.fillInStackTrace());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取一天的开始时间戳
     * @return
     */
    public static long getTodayBegin(){
        Calendar c1 = new GregorianCalendar();
        c1.set(Calendar.HOUR_OF_DAY, 0);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        return c1.getTimeInMillis();
    }

    /**
     * 获取一天的结束时间戳
     * @return
     */
    public static long getTodayEnd(){
        Calendar c2 = new GregorianCalendar();
        c2.set(Calendar.HOUR_OF_DAY, 23);
        c2.set(Calendar.MINUTE, 59);
        c2.set(Calendar.SECOND, 59);
        return  c2.getTimeInMillis();
    }

    public static String format(long time,String format){
        SimpleDateFormat sdf  = new SimpleDateFormat(format);
        String date = sdf.format(new Date(time));
        return date;
    }



}
